package com.example.materials_management.service;

import com.example.materials_management.entity.MaterialsBasicInfoEntity;
import com.example.materials_management.entity.MaterialsRemainingInfoEntity;
import com.example.materials_management.repository.MaterialsBasicInfoRepository;
import com.example.materials_management.repository.MaterialsRemainingInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MaterialsInfoService {
    @Autowired
    MaterialsBasicInfoRepository materialsBasicInfoRepository;
    @Autowired
    MaterialsRemainingInfoRepository materialsRemainingInfoRepository;

    public List<Map<String, Object>> getItems(
            Integer id,
            String name,
            String specification,
            String category,
            String measureUnit,
            Integer remainingAmountMin,
            Integer remainingAmountMax
    ) {
        return materialsBasicInfoRepository.findItems(id, name, specification, category, measureUnit, remainingAmountMin, remainingAmountMax);
    }

    @Modifying
    public Map<String, Object> addItems(List<MaterialsBasicInfoEntity> entities) {
        List<Integer> ids = materialsBasicInfoRepository.getIds();
        Integer maxId = Collections.max(ids);
        List<MaterialsBasicInfoEntity> errors = new ArrayList<>();
        List<MaterialsRemainingInfoEntity> materialsRemainingInfoEntities = new ArrayList<>();
        for (MaterialsBasicInfoEntity entity : entities) {
            if (entity.getId() == null || entity.getId() == 0) entity.setId(++maxId);
            else maxId = Math.max(maxId, entity.getId());
            materialsRemainingInfoEntities.add(new MaterialsRemainingInfoEntity(entity.getId(), 0));
            if (ids.contains(entity.getId())) errors.add(entity);
        }
        Map<String, Object> res = new HashMap<>();
        if (errors.size() > 0) {
            res.put("Status", "Error");
            res.put("Information", "Id already exists");
            res.put("Error Items", errors);
        } else {
            materialsBasicInfoRepository.saveAll(entities);
            materialsRemainingInfoRepository.saveAll(materialsRemainingInfoEntities);
            res.put("Status", "Success");
        }
        return res;
    }

    @Modifying
    public Map<String, Object> modifyItem(Integer id, MaterialsBasicInfoEntity entity) {
        List<Integer> ids = materialsBasicInfoRepository.getIds();
        Map<String, Object> res = new HashMap<>();
        if (!ids.contains(id)) {
            res.put("Status", "Error");
            res.put("Information", "Id does not exist");
        } else if (id != entity.getId()) {
            res.put("Status", "Error");
            res.put("Information", "Id does not equals entity's id");
        } else {
            materialsBasicInfoRepository.save(entity);
            res.put("Status", "Success");
        }
        return res;
    }
}
